'use strict';
const express = require('express')
const webpack = require('webpack');
const devConfig = require('../webpack.config');
const middleware = require('webpack-dev-middleware');
const proxy = require('koa-better-http-proxy');
const url = require('url');

module.exports = {
    entry: function (app, router, opts) {
        devConfig.plugins.push(
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify('development'),
                ...(() => {
                    let nextParams = {}
                    for (let k in opts.frontParams) {
                        nextParams[k] = JSON.stringify(opts.frontParams[k])
                    }
                    return nextParams
                })()
            }),
        )
  
        const compiler = webpack(devConfig);

        app.use(proxy('http://localhost:5501', {
            filter: function (ctx) {
                return /\/build/.test(url.parse(ctx.url).path);
            },
            proxyReqPathResolver: function (ctx) {
                return 'client' + url.parse(ctx.url).path;
            }
        }));

        app.use(proxy('http://localhost:5501', {
            filter: function (ctx) {
                return /\/$/.test(url.parse(ctx.url).path);
            },
            proxyReqPathResolver: function (ctx) {
                return 'client/build/index.html';
            }
        }));
   
        const server = express();
        server.use(middleware(compiler));
        //server.use(require("webpack-hot-middleware")(compiler));
        server.listen('5501', function (err) {
            if (err) {
                console.log(err);
            }
        })
    }
};
